package com.example.wanandroid;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.wanandroid.activity.SearchActivity;
import com.example.wanandroid.fragment.AppFragment;
import com.example.wanandroid.fragment.HomeFragment;
import com.example.wanandroid.fragment.TimerFragment;
import com.example.wanandroid.fragment.WallpaperFragment;
import static com.example.wanandroid.R.id.main_content_layout;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private Toolbar mToolBar;
    private NavigationView main_nav_view;
    private DrawerLayout main_drawer_layout;
    private HomeFragment homeFragment;
    private WallpaperFragment wallpaperFragment;
    private AppFragment appFragment;
    private TimerFragment timerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initToolBar();
        initView();
    }

    private void initToolBar() {
        mToolBar = (Toolbar) findViewById(R.id.mToolBar);
        setSupportActionBar(mToolBar);
    }

    private void initView() {
        main_drawer_layout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
        main_nav_view = (NavigationView) findViewById(R.id.main_nav_view);
        //设置监听
        main_nav_view.setNavigationItemSelectedListener(this);
        //去掉渲染
        main_nav_view.setItemIconTintList(null);
        ActionBarDrawerToggle toggle
                = new ActionBarDrawerToggle(this,
                main_drawer_layout, mToolBar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        //切换菜单的动画
        main_drawer_layout.addDrawerListener(toggle);
        //同步状态
        toggle.syncState();

        //显示默认的Fragment
        showHomeFragment();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_menu_home:
                showHomeFragment();
                break;
            case R.id.nav_menu_wallpaper:
                showWallpaperFragment();
                break;
            case R.id.nav_menu_app:
                showAppFragment();
                break;
            case R.id.nav_menu_time:
                showTimeFragment();
                break;
            case R.id.nav_menu_share:

                break;
            case R.id.nav_menu_setting:

                break;
        }
        //恢复
        main_drawer_layout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_san:

                break;
            case R.id.menu_http:

                break;
            case R.id.menu_share:

                break;
            case R.id.menu_setting:
                Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_search:
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showHomeFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            ft.add(main_content_layout, homeFragment);
        }
        hideAllFragment(ft);
        ft.show(homeFragment);
        ft.commit();
    }

    private void showWallpaperFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (wallpaperFragment == null) {
            wallpaperFragment = new WallpaperFragment();
            ft.add(main_content_layout, wallpaperFragment);
        }
        hideAllFragment(ft);
        ft.show(wallpaperFragment);
        ft.commit();
    }

    private void showAppFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (appFragment == null) {
            appFragment = new AppFragment();
            ft.add(main_content_layout, appFragment);
        }
        hideAllFragment(ft);
        ft.show(appFragment);
        ft.commit();
    }

    private void showTimeFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (timerFragment == null) {
            timerFragment = new TimerFragment();
            ft.add(main_content_layout, timerFragment);
        }
        hideAllFragment(ft);
        ft.show(timerFragment);
        ft.commit();
    }

    //隐藏所有的Fragment
    private void hideAllFragment(FragmentTransaction fb) {
        if (homeFragment != null) {
            fb.hide(homeFragment);
        }
        if (wallpaperFragment != null) {
            fb.hide(wallpaperFragment);
        }
        if (appFragment != null) {
            fb.hide(appFragment);
        }
        if (timerFragment != null) {
            fb.hide(timerFragment);
        }
    }

    @Override
    public void onAttachFragment(android.support.v4.app.Fragment fragment) {
        super.onAttachFragment(fragment);
        if (homeFragment == null && fragment instanceof HomeFragment) {
            homeFragment = (HomeFragment) fragment;
        } else if (wallpaperFragment == null && fragment instanceof WallpaperFragment) {
            wallpaperFragment = (WallpaperFragment) fragment;
        } else if (appFragment == null && fragment instanceof AppFragment) {
            appFragment = (AppFragment) fragment;
        } else if (timerFragment == null && fragment instanceof TimerFragment) {
            timerFragment = (TimerFragment) fragment;
        }
    }
}
